/* Serial driver */

/* Copyright (C) 2009-2011 David Zanetti
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; version 2 of the License.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License along
 *  with this program; if not, write to the Free Software Foundation, Inc.,
 *  51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 */


#include <avr/io.h>
#include <avr/pgmspace.h>
#include <stdio.h>
#include <avr/eeprom.h>
#include <avr/interrupt.h>
#include <util/atomic.h>
#include <stdlib.h>

#include "serial.h"

#include "global.h"
#include <util/delay.h>

#include "ringbuffer.h"

#define RUN_USART0_TX UCSR0B |= (1 << UDRIE0); 
#define RUN_USART1_TX UCSR1B |= (1 << UDRIE1); 

/* ring buffers */
char usart0_txring[256];
char usart0_rxring[32];
uint8_t usart0_tx_wp, usart0_rx_wp, usart0_tx_rp, usart0_rx_rp;
char usart1_txring[64];
char usart1_rxring[64];
uint8_t usart1_tx_wp, usart1_rx_wp, usart1_tx_rp, usart1_rx_rp;

#define RMASK0T 0xff
#define RMASK0R 0x1f

#define RMASK1 0x3f
#define RMASK1R 0x3f
#define RMASK1T 0x1f

/* interrupt handler for packets on TX USART0 */
ISR(USART0_UDRE_vect) {
	/* check to see if we have anything to send */
	if (!ring_readable_unsafe(usart0_tx_rp, usart0_tx_wp, RMASK0T)) {
		/* disable the interrupt and then exit, nothing more to do */
		UCSR0B &= ~(1 << UDRIE0);
		return;
	}
	/* TX the waiting packet */
	UDR0 = ring_read_unsafe(usart0_txring, &usart0_tx_rp, RMASK0T);
	return;
}

/* this handles packets coming into the serial port */
ISR(USART0_RX_vect) {
	char s;

	s = UDR0;

	ring_write_force_unsafe(usart0_rxring, &usart0_rx_rp, &usart0_rx_wp, RMASK0R,
			s);
#if defined(USART0_ECHO)
	ring_write_force_unsafe(usart0_txring, &usart0_tx_rp, &usart0_tx_wp, RMASK0T,
			s);
	UCSR0B |= (1 << UDRIE0); /* and make sure we're writing this out */
#endif

	flag_serial |= FLAG_SERIAL_U0_RX; /* indicate to main loops we had an RX */

#if defined(USART0_IS_CONSOLE)
	if (s == '\r') {
		flag_serial |= FLAG_SERIAL_U0_CMD; /* and we got a completed command */
	}
#endif
}

/* interrupt handler for packets on TX USART1 */
ISR(USART1_UDRE_vect) {
	/* check to see if we have anything to send */
	if (!ring_readable_unsafe(usart1_tx_rp, usart1_tx_wp, RMASK1T)) {
		/* disable the interrupt and then exit, nothing more to do */
		UCSR1B &= ~(1 << UDRIE1);
		return;
	}
	/* TX the waiting packet */
	UDR1 = ring_read_unsafe(usart1_txring, &usart1_tx_rp, RMASK1T);
	return;
}

/* this handles packets coming into the serial port */
ISR(USART1_RX_vect) {
	char s = 0;

	s = UDR1;

	ring_write_force_unsafe(usart1_rxring, &usart1_rx_rp, &usart1_rx_wp, RMASK1R,
			s);
#if defined(USART1_ECHO)
	ring_write_force_unsafe(usart1_txring, &usart1_tx_rp, &usart1_tx_wp, RMASK0T,
			s);
	UCSR1B |= (1 << UDRIE1); /* and make sure we're writing this out */
#endif

	flag_serial |= FLAG_SERIAL_U1_RX; /* indicate to main loops we had an RX */

#if defined(USART1_IS_CONSOLE)
	if (s == '\r') {
		flag_serial |= FLAG_SERIAL_U1_CMD; /* and we got a completed command */
	}
#endif
}

void serial0_init(uint16_t baud) {
	uint16_t ubrr;

	/* init ringbuffer pointers */
	usart0_tx_wp = 0;
	usart0_rx_wp = 0;
	usart0_tx_rp = 0;
	usart0_rx_rp = 0;

	/* now calculate baud rat */
	UCSR0A |= (1 << U2X0);
	ubrr = ((F_CPU / (baud*8UL))-1);
	UBRR0H = (ubrr >> 8);
	UBRR0L = (ubrr & 0xff);

	/* enable TX, RX, RX Interrupt */
	UCSR0B |= (1 << RXEN0) | (1 << TXEN0) | (1 << RXCIE0);
}

void serial1_init(uint16_t baud) {
	uint16_t ubrr;

	usart1_tx_wp = 0;
	usart1_rx_wp = 0;
	usart1_tx_rp = 0;
	usart1_rx_rp = 0;

	/* now calculate baud rat */
	UCSR1A |= (1 << U2X1);
	ubrr = ((F_CPU/(baud*8UL))-1);
	UBRR1H = (ubrr >> 8);
	UBRR1L = (ubrr & 0xff);

	/* eanble TX/RX */
	UCSR1B |= (1 << RXEN1) | (1 << TXEN1) | (1 << RXCIE1);

}

uint8_t serial0_tx_cout(char s) {
	if (!ring_writeable(usart0_tx_rp, usart0_tx_wp, RMASK0T)) {
		return 0;
	}
	ring_write(usart0_txring,&usart0_tx_wp, RMASK0T, s);

	RUN_USART0_TX;

	return 1;
}

uint8_t serial1_tx_cout(char s) {
	if (!ring_writeable(usart1_tx_rp, usart1_tx_wp, RMASK1)) {
		return 0;
	}
	ring_write(usart1_txring,&usart1_tx_wp, RMASK1, s);

	RUN_USART1_TX;

	return 1;
}

uint8_t serial0_tx(const char *str, uint8_t len) {
	/* loop over each char in the buffer */
	while (len) {
		/* buiffer isn't full */
		if (!ring_writeable(usart0_tx_rp, usart0_tx_wp, RMASK0T)) {
			return 0;
		}
		/* write char */
		ring_write(usart0_txring,&usart0_tx_wp, RMASK0T, *str++);
		/* move on */
		len--;
	}

	RUN_USART0_TX;

	return 1;
}

uint8_t serial1_tx(const char *str, uint8_t len) {
	/* loop over each char in the buffer */
	while (len) {
		/* buiffer isn't full */
		if (!ring_writeable(usart1_tx_rp, usart1_tx_wp, RMASK1T)) {
			return 0;
		}
		/* write char */
		ring_write(usart1_txring,&usart1_tx_wp, RMASK1T, *str++);
		/* move on */
		len--;
	}

	RUN_USART1_TX;

	return 1;
}

uint8_t serial0_tx_PGM(const char *str) {
	uint8_t n = 0;
	char c;
	/* loop over each char in the buffer */
	while ((c = pgm_read_byte(&str[n]))) {
		/* if we would be equal to the read pointer when
     we increment the pointer, then we must discard
     the rest of the text - the TX buffer is full */
		if (!ring_writeable(usart0_tx_rp, usart0_tx_wp,RMASK0T)) {
			return 0; /* FAILURE */
		}
		/* okay, we can write this char into the buffer */
		ring_write(usart0_txring, &usart0_tx_wp, RMASK0T, c);
		n++;
	}

	RUN_USART0_TX;

	return 1;
}

uint8_t serial1_tx_PGM(const char *str) {
	uint8_t n = 0;
	char c;
	/* loop over each char in the buffer */
	while ((c = pgm_read_byte(&str[n]))) {
		/* if we would be equal to the read pointer when
     we increment the pointer, then we must discard
     the rest of the text - the TX buffer is full */
		if (!ring_writeable(usart1_tx_rp, usart1_tx_wp, RMASK1)) {
			return 0; /* FAILURE */
		}
		/* okay, we can write this char into the buffer */
		ring_write(usart1_txring, &usart1_tx_wp, RMASK1, c);
		n++;
	}

	RUN_USART1_TX;

	return 1;
}

/* return a character from the ring buffer */
uint8_t serial0_rx(void) {
	char c;
	/* read out the char, moving the read pointer */

	c = ring_read(usart0_rxring, &usart0_rx_rp, RMASK0R);

	if (!(ring_readable(usart0_rx_rp,usart0_rx_wp,RMASK0R))) {
		clear_flag(flag_serial,FLAG_SERIAL_U0_RX);
	}

	return c;
}

/* return a character from the ring buffer */
uint8_t serial1_rx(void) {
	char c;
	/* read out the char, moving the read pointer */

	c = ring_read(usart1_rxring, &usart1_rx_rp, RMASK1);

	if (!(ring_readable(usart1_rx_rp,usart1_rx_wp,RMASK1))) {
		clear_flag(flag_serial,FLAG_SERIAL_U1_RX);
	}

	return c;
}


uint8_t serial0_tx_hex(uint8_t c) {
	char hex[16] = "0123456789abcdef";
	serial0_tx_cout(hex[c >> 4]);
	serial0_tx_cout(hex[c & 0xf]);
	return 0;
}

uint8_t serial1_tx_hex(uint8_t c) {
	char hex[16] = "0123456789ABCDEF";
	serial1_tx_cout(hex[c >> 4]);
	serial1_tx_cout(hex[c & 0xf]);
	return 0;
}


uint8_t serial0_tx_dec(uint32_t s) {
	char a[11] = ""; /* 4294967296 */
	uint8_t n = 0;

	ultoa(s, a, 10);

	while (a[n]) {
		serial0_tx_cout(a[n]);
		n++;
	}

	return 0;

}

uint8_t serial0_tx_cr(void) {
	serial0_tx_PGM(PSTR("\r\n"));
	return 0;
}

uint8_t serial1_cr(void) {
	serial1_tx_PGM(PSTR("\r\n"));
	return 0;
}
